<?php

namespace App\Service\SiteLocation\Import;

use App\Models\Common\MapNationalBrandShopModel;
use App\Models\OpenShop\Common\MallSettleInShopInfoModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class BrandShop
{
    public function collection(Collection $collection, ?Command $command)
    {
        $num = 0;
        $collection->first()->map(function ($brand) use (&$num) {
            if ($num == 0) {
                $header = [
                    "名称",
                    "地址",
                    "子品牌",
                    "门店级别",
                    "经营模式",
                    "电话",
                    "外卖",
                    "特性",
                    "经营时间",
                    "开门时间",
                    "关门时间",
                    "国家",
                    "省份",
                    "城市",
                    "区县",
                    "开业日期",
                    "城市级别",
                    "所处位置大类",
                    "所处位置小类",
                    "所处位置名称",
                    "位置周边大类",
                    "位置周边小类",
                    "位置周边名称集",
                    "经度",
                    "纬度",
                    "品牌",
                ];
                foreach ($header as $key => $value) {
                    if ($brand[$key] != $value) {
                        dd('列顺序错误');
                    }
                }
                $num++;
                return;
            }

            if ($brand[0] == null) {
                return;
            }

            $model = MapNationalBrandShopModel::query()->firstOrNew([
                'brand' => $brand[25],
                'name'  => $brand[0],
                'lon'   => $brand[23],
                'lat'   => $brand[24],
            ]);

            if (!$model->exists) {
                // 手动保存新实例到数据库
                $model->save();
            }
            $model->wgNumber    = 'PP77' . str_pad($model->id, 7, '0', STR_PAD_LEFT);
            $model->status      = 0;
            $model->type        = 2;
            $model->address     = $brand[1] ?: '';
            $model->province    = $brand[12] ?: '';
            $model->city        = $brand[13] ?: '';
            $model->district    = $brand[14] ?: '';
            $model->openingTime = $brand[15] ?: '';
            $model->star        = 0;
            $model->save();
            dump($num);
            $num++;
        });
    }
}
